package cn.circle.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.circle.system.dao.ISysMenusDao;
import cn.circle.system.model.SysMenus;
import cn.circle.system.pagemodel.DataGrid;
import cn.circle.system.pagemodel.PageHelper;
import cn.circle.system.pagemodel.TreeNode;
import cn.circle.system.service.ISysMenusService;
import cn.circle.system.util.HqlHelper;
import cn.circle.system.util.TreeUtil;


/*import cn.circle.system.dao.ISysMenusDao;
import cn.circle.system.model.SysMenuActions;
import cn.circle.system.model.SysMenus;
import com.linewell.system.pagemodel.DataGrid;
import com.linewell.system.pagemodel.PageHelper;
import com.linewell.system.pagemodel.TreeNode;
import com.linewell.system.service.ISysMenusService;
import com.linewell.system.utils.HqlHelper;
import com.linewell.system.utils.TreeUtil;*/

@Service
public class SysMenusServiceImpl implements ISysMenusService {
	@Autowired
	private  ISysMenusDao sysMenusDao;

	@Override
	public DataGrid datagrid(PageHelper ph) {
		DataGrid dg = new DataGrid();
		// 用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();

		// 注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer("from SysMenus t where 1=1 ");

		//hql = HqlHelper.orderHql(hql, ph); // 排序
		List<SysMenus> lstAll = sysMenusDao.find(hql.toString(), params, ph.getPage(), ph.getRows());

		List<TreeNode> lstTree = null;
		if (lstAll != null && lstAll.isEmpty() == false) {
			TreeUtil treeUtil = new TreeUtil();
			treeUtil.setLstAll(lstAll);
			lstTree = treeUtil.findById(null);
		}
		dg.setRows(lstTree);
		dg.setTotal(sysMenusDao.count("select count(*) " + hql, params));
		return dg;
	}

	@Override
	public boolean removeAll(String id) {
		String hql = "delete SysMenus s where s.id in (" + id + ") or s.parentId in (" + id + ")";
		return (sysMenusDao.executeHql(hql)) > 0;
	}

	@Override
	public List<SysMenus> getMenus(Integer type) {
		String hql = " from SysMenus where status = 9 and type < " + type + " order by showOrder desc ";
		return sysMenusDao.find(hql);
	}

	@Override
	public boolean save(SysMenus sysMenus) {
		sysMenusDao.save(sysMenus);
		return false;
	}

	@Override
	public List<SysMenus> get(String id) {
		String hql = "from SysMenus s where s.id=" + id;
		return sysMenusDao.find(hql);
	}

	@Override
	public boolean update(SysMenus c) {

		StringBuffer hql = new StringBuffer("update SysMenus s set ");

		// 拼接修改条件
		if (c.getType() != null && !"".equals(c.getType())) {
			hql.append("  s.type = " + c.getType() + " ");
		}
		if (c.getParentId() != null && !"".equals(c.getParentId())) {
			hql.append(" , s.parentId = " + c.getParentId() + " ");
		}

		if (c.getName() != null && !"".equals(c.getName())) {
			hql.append(" , s.name = '" + c.getName() + "' ");
		}

		if (c.getUrl() != null) {
			hql.append(" , s.url = '" + c.getUrl() + "' ");
		}

		if (c.getShowOrder() != null && !"".equals(c.getShowOrder())) {
			hql.append(" , s.showOrder = " + c.getShowOrder() + " ");
		}
		if (c.getImg() != null) {
			hql.append(" , s.img = '" + c.getImg() + "' ");
		}
		if (c.getStatus() != null && !"".equals(c.getStatus())) {
			hql.append(" , s.status = " + c.getStatus() + " ");
		}

		hql.append(" where s.id =" + c.getId() + " ");

		return (sysMenusDao.executeHql(hql.toString())) > 0;
	}

	@Override
	public boolean openMenu(String id) {
		String hql = "update SysMenus s set s.status = 9 where s.id in (" + id + ") or s.parentId in (" + id + ")";
		return (sysMenusDao.executeHql(hql)) > 0;
	}

	@Override
	public boolean closeMenu(String id) {
		String hql = "update SysMenus s set s.status = 1 where s.id in (" + id + ") or s.parentId in (" + id + ")";
		return (sysMenusDao.executeHql(hql)) > 0;
	}

	/*@Override
	public List<SysMenuActions> getAction(String id) {
		return sysMenusDao.getAction(id);
	}
*/
	@Override
	public boolean delete(Integer id) {
		return sysMenusDao.deleteOne(id);
	}

	@Override
	public boolean saveUrl(String url, Integer id) {
		// TODO Auto-generated method stub
		return sysMenusDao.saveUrl(url,id);
	}

	@Override
	public boolean saveOrUpdate(SysMenus sysMenus) {
		sysMenusDao.saveOrUpdate(sysMenus);
		return true;
	}
}
